package com.hcx.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcx.reggie.pojo.Employee;
import com.hcx.reggie.service.EmployeeService;
import com.hcx.reggie.utils.R;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.InstanceFilter;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("employee")
@CrossOrigin
public class EmployeeController {
    private EmployeeService employeeService;

    @Autowired
    public EmployeeController(EmployeeService employeeService){
        this.employeeService = employeeService;
    }


    /**
     * 员工的登录
     * @param request
     * @param employee
     * @return
     */

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        String password = employee.getPassword();
        String md5pw = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        Employee target = employeeService.getOne(wrapper);
        if(target !=null ){
            if (target.getPassword().equals(md5pw)){
               if (target.getStatus() == 1){
                    request.getSession().setAttribute("employee",target.getId());
                   return R.success(target);

               }else {
                   return R.error("account had been banned");
               }

            }else {
                return R.error("password error");
            }
        }
        return R.error("name error");
    }

    /**
     * 员工退出登录
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("logout success");
    }


    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping()
    public R<String> addEmployee(HttpServletRequest request, @RequestBody Employee employee){
        String username = employee.getUsername();
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(wrapper);
        if(emp != null){
            return R.error("账号已存在T^T");
        }
        log.info("添加员工");
        String pwd = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(pwd);
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser((Long) request.getSession().getAttribute("employee"));
//        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employeeService.save(employee);
        return R.success("保存成功！");
    }

    /**
     * 员工信息的分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page> page(int page ,int pageSize,String name){
        Page iPage = new Page(page,pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper();
        if(name != null) {
            wrapper.like(Employee::getName, name);
        }
        wrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(iPage, wrapper);
        return R.success(iPage);
    }

    /**
     * 根据员工id修改员工的信息
     * @param employee
     * @return
     */
    @PutMapping()
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee){
        log.info("update");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 修改用户回显
     * @param id
     * @return
     */

    @GetMapping("{id}")
    public R<Employee> query(@PathVariable Long id){
        Employee byId = employeeService.getById(id);
        if(byId != null) {
            return R.success(byId);
        }
        return R.error("该用户已被删除");
    }




}
